<?php

namespace app\admin\controller\recruit;

use app\common\controller\Backend;
use fast\Tree;
use think\helper\Time;

/**
 * 会员规则管理
 *
 * @icon fa fa-circle-o
 */
class Step extends Backend
{

    /**
     * Ad模型对象
     */
    protected $model = null;
    protected $adlist = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Step');
    }

    /**
     * 查看
     */
    public function index()
    {
        $recruit_id = $this->request->param('ids');
        $this->view->assign('id',$recruit_id);//recruit_id
        $title=model('Recruit')->where('recruit_id', $recruit_id)->value('recruit_title');//recruit_id
        $this->view->assign('content',$title);

        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model->where($where)->where('recruit_id='.$recruit_id)->order($sort, $order)->count();
            $list = $this->model->where($where)->where('recruit_id='.$recruit_id)->order('sort asc')->limit($offset, $limit)->select();
            foreach($list as &$step){
                $step['recruit_title']=model('Recruit')->where('recruit_id',$step['recruit_id'])->value('recruit_title');
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    //$result = $this->model->allowField(true)->save($params);
                    $result=db('hs_recruit_step')->insertGetId($params);
                    if ($result !== false) {
                        //判断修改当前流程
                        if($params['status']=='doing'){
                            $last_id = $result;
                            $rec=array();
                            $rec['step_id']=$last_id;
                            $rec['recruit_id']=$params['recruit_id'];
                            $res=db('hs_recruit')->update($rec);
                        }
                        $this->success('添加成功',url('add'),'',1);
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
       $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        //判断修改当前流程
                        if($params['status']=='doing'&&$params['step_v_status']!='doing'){
                            $rec=array();
                            $rec['step_id']=$params['step_v_id'];
                            $rec['recruit_id']=$params['recruit_id'];
                            $res=db('hs_recruit')->update($rec);
                        }
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $delIds = array_unique(explode(',', $ids));
            $count = $this->model->where('step_id', 'in', $delIds)->delete();
            if ($count)
            {
                $this->success();
            }
        }
        $this->error();
    }

}
